package com.houseliker.activity;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.houseliker.R;

import java.util.ArrayList;
import java.util.List;

import github.chenupt.springindicator.SpringIndicator;
import github.chenupt.springindicator.viewpager.ScrollerViewPager;

/**
 * Created by sunrx on 2016/4/22.
 */
public class GuideActivity extends BaseActivity {
    private static final String TAG = "Guide";
    
    private ScrollerViewPager vp_guide;
    private List<Integer> imageResList;
    private List<String> titleList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
        setView(R.layout.activity_guide, TYPE_NORMAL);

        vp_guide = (ScrollerViewPager) findViewById(R.id.vp_guide);
        SpringIndicator springIndicator = (SpringIndicator) findViewById(R.id.indicator);

        imageResList = new ArrayList<>();
        imageResList.add(R.mipmap.guide_1);
        imageResList.add(R.mipmap.guide_1);
        imageResList.add(R.mipmap.guide_1);
        imageResList.add(R.mipmap.guide_1);

        titleList = new ArrayList<>();
        titleList.add("1");
        titleList.add("2");
        titleList.add("3");
        titleList.add("4");

        ViewPagerAdapter adapter = new ViewPagerAdapter();
//        PagerModelManager manager = new PagerModelManager();
//        manager.addCommonFragment(GuideFragment.class, imageResList, titleList);
//        ModelPagerAdapter adapter = new ModelPagerAdapter(getSupportFragmentManager(), manager);
        vp_guide.setAdapter(adapter);
        vp_guide.fixScrollSpeed();
        springIndicator.setViewPager(vp_guide);

        vp_guide.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                Log.i(TAG, "onPageScrolled: "+position);
            }

            @Override
            public void onPageSelected(int position) {
                Log.i(TAG, "onPageSelected: "+position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                Log.i(TAG, "onPageScrollStateChanged: "+state);
            }
        });
    }

    private class ViewPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return imageResList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titleList.get(position);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(mContext);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            imageView.setImageResource(imageResList.get(position));
            container.addView(imageView);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
